<template>
  <div class="app-container" v-loading="fxLoading">
    <h2 style="text-align: center">字体和七步法</h2>

    <el-steps
      :active="4"
      process-status="wait"
      align-center
      style="margin-bottom: 40px"
    >
      <el-step title="添加字体" />
      <el-step title="添加七步" />
    </el-steps>
    <div>
      <el-form :inline="true">
        <el-form-item>
          <el-select
            v-model="buzhou"
            placeholder="请选择类型"
            @change="buzhouChange"
          >
            <el-option label="欣赏" :value="0"> </el-option>
            <el-option label="认读" :value="1"> </el-option>
            <el-option label="观察" :value="2"> </el-option>
            <el-option label="分析" :value="4"> </el-option>
            <el-option label="笔法" :value="3"> </el-option>
            <el-option label="视频" :value="7"> </el-option>
            <el-option label="对比" :value="5"> </el-option>
            <el-option label="随堂练习" :value="6"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <el-form
      :rules="dataRule"
      ref="analyzeForm"
      :model="analyze"
      label-width="120px"
    >
      <el-form-item label="笔画特征" prop="stokesFeature">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入笔画特征"
          v-model="analyze.stokesFeature"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="空间特征" prop="spaceFeature">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入空间特征"
          v-model="analyze.spaceFeature"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="分析字体图片" prop="analyzePicture">
        <el-upload
          class="avatar-uploader"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :show-file-list="false"
          :on-success="handleAvatarSuccess1"
        >
          <img
            v-if="analyze.analyzePicture"
            :src="analyze.analyzePicture"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="笔画特征语音" prop="stokesFeatureSounds">
        <el-upload
          class="upload-uploader"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :on-exceed="handleExceed"
          :on-remove="handleRemove"
          :on-success="handleAudioSuccess"
          :before-upload="beforeAudioUpload"
          :show-file-list="false"
        >
          <audio :src="analyze.stokesFeatureSounds" controls="controls">
            您的浏览器不支持音频播放
          </audio>
        </el-upload>
      </el-form-item>
      <el-form-item label="空间特征语音" prop="spaceFeatureSounds">
        <el-upload
          class="upload-uploader"
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :on-exceed="handleExceed"
          :on-remove="handleRemove1"
          :on-success="handleAudioSuccess1"
          :before-upload="beforeAudioUpload"
          :show-file-list="false"
        >
          <audio :src="analyze.spaceFeatureSounds" controls="controls">
            您的浏览器不支持音频播放
          </audio>
        </el-upload>
      </el-form-item>
    </el-form>

    <div>
      <el-button @click="previous">上一步</el-button>
      <el-button :disabled="saveBtnDisabled" type="primary" @click="next"
        >保存</el-button
      >
    </div>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      buzhou: 4,
      fxLoading: false,
      saveBtnDisabled: false, // 保存按钮是否禁用
      analyze: {
        // 出版社对象
        analyzePicture: "",
        stokesFeature: "",
        spaceFeature: "",
        stokesFeatureSounds: "",
        spaceFeatureSounds: "",
        tid: null,
        id: null,
      },
      xzBtn: false,
      type: 0, // 0:添加1:修改
      dialogObserveVisible: false,
      saveObserveBtnDisabled: false,
      dialogObservePictureVisible: false,
      saveRdPictureBtnDisabled: false,
      dataRule: {
        stokesFeature: [
          { required: true, message: "笔画特征不能为空", trigger: "blur" },
        ],
        spaceFeature: [
          { required: true, message: "空间特征不能为空", trigger: "blur" },
        ],
        stokesFeatureSounds: [
          { required: true, message: "笔画特征语音不能为空" },
        ],
        spaceFeatureSounds: [
          { required: true, message: "空间特征语音不能为空" },
        ],
        analyzePicture: [{ required: true, message: "图片不能为空" }],
      },
      dataRule1: {
        porder: [{ required: true, message: "顺序不能为空", trigger: "blur" }],
        analyzePictureDescription: [
          { required: true, message: "图片描述不能为空", trigger: "blur" },
        ],
      },
    };
  },

  activated() {
    this.buzhou = 4;
    this.analyze.tid = this.$route.params.id;
    // 有数据就要回显数据
    this.getAnalyzeByWordId();
  },

  methods: {
    buzhouChange(value) {
      switch (value) {
        case 0:
          // 欣赏
          this.$router.push({
            path: "/ruanbi/ruanbixsAdd/" + this.$route.params.id,
          });
          break;
        case 1:
          // 认读
          this.$router.push({
            path: "/ruanbi/ruanbirdAdd/" + this.$route.params.id,
          });
          break;
        case 2:
          // 观察
          this.$router.push({
            path: "/ruanbi/ruanbigcAdd/" + this.$route.params.id,
          });
          break;
        case 3:
          // 笔法
          this.$router.push({
            path: "/ruanbi/ruanbibfAdd/" + this.$route.params.id,
          });
          break;
        case 4:
          // 分析
          this.$router.push({
            path: "/ruanbi/ruanbifxAdd/" + this.$route.params.id,
          });
          break;
        case 5:
          // 对比
          this.$router.push({
            path: "/ruanbi/ruanbidbAdd/" + this.$route.params.id,
          });
          break;
        case 6:
          // 随堂练习
          this.$router.push({
            path: "/ruanbi/ruanbistlxAdd/" + this.$route.params.id,
          });
          break;
        default:
          // 视频
          this.$router.push({
            path: "/ruanbi/wordVideo/" + this.$route.params.id,
          });
          break;
      }
    },
    beforeAudioUpload(file) {
      const fileName = file.name;
      const fileType = fileName.substring(fileName.lastIndexOf("."));
      if (fileType !== ".mp3") {
        this.$message.error("上传音频只能是mp3!");
        return false;
      }
      return true;
    },
    //上传成功后的回调函数
    handleAvatarSuccess1(response) {
      this.analyze.analyzePicture = response.url;
    },
    //上传成功后的回调函数
    handleAudioSuccess(response) {
      this.analyze.stokesFeatureSounds = response.url;
    },
    //上传成功后的回调函数
    handleAudioSuccess1(response) {
      this.analyze.spaceFeatureSounds = response.url;
    },
    // 超出上传限制
    handleExceed(files, fileList) {
      this.$message.warning(`只能上传一个音频`);
    },
    // 删除音频
    handleRemove(file, fileList) {
      this.analyze.stokesFeatureSounds = "";
    },
    // 删除音频
    handleRemove1(file, fileList) {
      this.analyze.spaceFeatureSounds = "";
    },
    getAnalyzeByWordId() {
      this.fxLoading = true;
      this.$http({
        url: this.$http.adornUrl(`/sf/cbanalyze/info/${this.analyze.tid}`),
        method: "get",
      }).then(({ data }) => {
        this.$refs.analyzeForm.resetFields();
        this.analyze.id = null;
        this.analyze.tid = this.$route.params.id;
        this.xzBtn = false;
        if (data && data.code === 0) {
          if (data.cbAnalyze) {
            console.log(data);
            this.type = 1;
            this.analyze = data.cbAnalyze;
            this.xzBtn = true;
          } else {
            this.type = 0;
          }
        } else {
          // this.analyze = { typefaceName: "", editionCover: "", id: null };
        }
        this.fxLoading = false;
      });
    },
    // 章节对话框中的保存或修改
    saveOrUpdate() {
      if (this.type == 0) {
        // 添加章节
        this.analyzeFormSubmit();
      } else {
        // 修改章节
        this.updateAnalyze();
      }
    },
    // 更新章节图片信息
    updateAnalyze() {
      this.$refs["analyzeForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbanalyze/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.analyze.id,
                tid: this.analyze.tid,
                analyzePicture: this.analyze.analyzePicture,
                stokesFeature: this.analyze.stokesFeature,
                stokesFeatureSounds: this.analyze.stokesFeatureSounds,
                spaceFeatureSounds: this.analyze.spaceFeatureSounds,
                spaceFeature: this.analyze.spaceFeature,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getAnalyzeByWordId();
                  this.dialogObserveVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 打开对话框，添加、修改
    addObserve(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.analyzeForm.resetFields();
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.analyze.id = null;
        this.analyze.characterFeatureSrc = "";
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbanalyze/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.analyze.id = data.cbAnalyze.id;
          this.analyze.characterFeature = data.cbAnalyze.characterFeature;
          this.analyze.tid = data.cbAnalyze.tid;
          this.analyze.characterFeatureSrc = data.cbAnalyze.characterFeatureSrc;
        });
      }
      this.dialogObserveVisible = true;
    },
    // 表单提交
    analyzeFormSubmit() {
      // 表单验证
      this.$refs["analyzeForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbanalyze/save`),
            method: "post",
            data: this.$http.adornData(
              {
                tid: this.analyze.tid,
                analyzePicture: this.analyze.analyzePicture,
                stokesFeature: this.analyze.stokesFeature,
                stokesFeatureSounds: this.analyze.stokesFeatureSounds,
                spaceFeatureSounds: this.analyze.spaceFeatureSounds,
                spaceFeature: this.analyze.spaceFeature,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getAnalyzeByWordId();
                  // 只能添加一次
                  this.xzBtn = true;
                  this.dialogObserveVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 上一步
    previous() {
      this.$router.push({
        path: "/ruanbi/word7",
      });
    },
    next() {
      this.saveOrUpdate();
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 400px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
